package com.xunfang.shareaddress;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xunfang.shareaddress.method.Communicates;

/**
 * 主界面
 */
public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	// 定义变量和组件
	LocationManager loc;
	Location locaction;
	TextView jingdu, weidu, gaodu, sudu, fangxiang;
	Button shareAddress, showMapAddress, showOtherShare;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 实例化组件
		jingdu = (TextView) findViewById(R.id.jingdu);
		weidu = (TextView) findViewById(R.id.weidu);
		gaodu = (TextView) findViewById(R.id.gaodu);
		sudu = (TextView) findViewById(R.id.sudu);
		fangxiang = (TextView) findViewById(R.id.fangxiang);

		shareAddress = (Button) findViewById(R.id.shareAddress);
		showMapAddress = (Button) findViewById(R.id.showMapAddress);
		showOtherShare = (Button) findViewById(R.id.showOtherShare);
		// 设置监听事件
		shareAddress.setOnClickListener(new OnButtonClick());
		showMapAddress.setOnClickListener(new OnButtonClick());
		showOtherShare.setOnClickListener(new OnButtonClick());

		// 创建LocationManager对象
		loc = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// 创建Location对象 从GPS中获取定位信息
		locaction = loc.getLastKnownLocation(LocationManager.GPS_PROVIDER);

		updateview(locaction);
		// 设置3秒获取一次GPS定位信息
		loc.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8,
				new LocationListener() {

					public void onStatusChanged(String provider, int status,
							Bundle extras) {
						// TODO Auto-generated method stub
					}

					public void onProviderEnabled(String provider) {
						// TODO Auto-generated method stub
						// 更新位置信息
						updateview(loc.getLastKnownLocation(provider));
					}

					public void onProviderDisabled(String provider) {
						// TODO Auto-generated method stub
						updateview(null);
						Intent intent = new Intent("Gps_State");
						intent.putExtra("nusing", "无法获取当前位置信息！");
						// 发送广播
						sendBroadcast(intent);
						System.out.println("不可用");
					}

					public void onLocationChanged(Location location) {
						// TODO Auto-generated method stub
						// 更新位置信息
						updateview(locaction);
					}
				});
	}

	class OnButtonClick implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			if (locaction != null) {
				switch (v.getId()) {
				// 共享位置信息
				case R.id.shareAddress:
					Intent intent = getIntent();
					// 获取当前用户的用户名
					String username = intent.getStringExtra("username");
					// 创建通信类对象
					Communicates communicates = new Communicates();
					// 将当前用户的地址发送到服务器
					communicates.sendAddress(username, jingdu.getText()
							.toString(), weidu.getText().toString());
					break;
				// 在地图上显示当前位置
				case R.id.showMapAddress:
					if (locaction != null) {
						Intent intentu = getIntent();
						String ausername = intentu.getStringExtra("username");
						Intent intentsm = new Intent(MainActivity.this,
								ShowMapActivity.class);
						intentsm.putExtra("key", "look");
						intentsm.putExtra("username", "lz");
						intentsm.putExtra("username", ausername);
						intentsm.putExtra("jd",
								Double.toString(locaction.getLongitude()));
						intentsm.putExtra("wd",
								Double.toString(locaction.getLatitude()));
						startActivity(intentsm);
					}
					break;
				case R.id.showOtherShare:
					Communicates communicate = new Communicates();
					List<String> list = new ArrayList<String>();
					list.addAll(communicate.sendRequst());
					Intent intents = getIntent();
					String ausernames = intents.getStringExtra("username");
					// 替换当前用户原来的位置信息
					for (int i = 0; i < list.size(); i = i + 3) {
						if (list.get(i).equals(ausernames)) {
							list.set(i + 1, jingdu.getText().toString());
							list.set(i + 2, weidu.getText().toString());
						}
					}

					Intent intentss = new Intent(MainActivity.this,
							ShowMapActivity.class);
					intentss.putExtra("key", "Share");
					intentss.putExtra("username", ausernames);
					intentss.putStringArrayListExtra("ShareAddress",
							(ArrayList<String>) list);
					intentss.putExtra("jd",
							Double.toString(locaction.getLongitude()));
					intentss.putExtra("wd",
							Double.toString(locaction.getLatitude()));
					startActivity(intentss);
					break;
				default:
					break;
				}
			} else {
				Toast.makeText(MainActivity.this, "当前位置信息为空！", 1).show();
			}
		}

	}

	// 用于显示当前位置信息
	public void updateview(Location newLocation) {
		if (newLocation != null) {
			jingdu.setText(locaction.getLongitude() + "");
			weidu.setText(locaction.getLatitude() + "");
			gaodu.setText(locaction.getAltitude() + "");
			sudu.setText(locaction.getSpeed() + "");
			fangxiang.setText(locaction.getBearing() + "");
		} else {
			System.out.println("newLocation is null");
			Intent intents = new Intent("Gps_State");
			intents.putExtra("nusing", "无法获取当前位置信息！");
			sendBroadcast(intents);
		}
	}
}